﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using iBook2.Models;

namespace iBook2.Controllers
{
    public class BookController : ApiController
    {
        private iBookEntities db = new iBookEntities();

        // GET api/Book
        public IEnumerable<Sach> GetSaches()
        {
            var saches = db.Saches.Include(s => s.DanhMuc).Include(s => s.NhaXuatBan);
            return saches.AsEnumerable();
        }

        // GET api/Book/5
        public Sach GetSach(string id)
        {
            Sach sach = db.Saches.Find(id);
            if (sach == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            return sach;
        }

        // PUT api/Book/5
        public HttpResponseMessage PutSach(string id, Sach sach)
        {
            if (ModelState.IsValid && id == sach.MaSach)
            {
                db.Entry(sach).State = EntityState.Modified;

                try
                {
                    db.SaveChanges();
                }
                catch (DbUpdateConcurrencyException)
                {
                    return Request.CreateResponse(HttpStatusCode.NotFound);
                }

                return Request.CreateResponse(HttpStatusCode.OK);
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // POST api/Book
        public HttpResponseMessage PostSach(Sach sach)
        {
            if (ModelState.IsValid)
            {
                db.Saches.Add(sach);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, sach);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = sach.MaSach }));
                return response;
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // DELETE api/Book/5
        public HttpResponseMessage DeleteSach(string id)
        {
            Sach sach = db.Saches.Find(id);
            if (sach == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            db.Saches.Remove(sach);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            return Request.CreateResponse(HttpStatusCode.OK, sach);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}